﻿using Anke.SHManage.BLL;
using Anke.SHManage.Model;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.IM.Controllers
{
    public class I_InvoiceApplyController : Controller
    {
        //
        // GET: /IM/I_InvoiceApply/

        public ActionResult I_InvoiceApplyList()
        {
            this.ViewData["Name"] = UserOperateContext.Current.Session_UsrInfo.Name;
            this.ViewData["UserID"] = UserOperateContext.Current.Session_UsrInfo.WorkCode;
            if (UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID == null)
            { this.ViewData["CenterID"] = 0; }
            else
            { this.ViewData["CenterID"] = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID; }
            this.ViewData["StationID"] = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;
            this.ViewData["start"] = DateTime.Now.AddDays(1 - DateTime.Now.Day).ToString("yyyy-MM-dd 00:00:00");
            this.ViewData["end"] = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd 00:00:00");
            return View();
        }
        /// <summary>
        /// 保存申领单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveApply()
        {
            string sDetailInfo = Request.Form["sApplyInfo"].ToString();
            I_InvoiceApply info = JsonHelper.GetJsonInfoBy<I_InvoiceApply>(sDetailInfo);
            string ApplyUserID = Request.Form["ApplyUserID"].ToString(); 
            string ApplyUserName = Request.Form["ApplyUserName"].ToString();   
            int CenterCode = Convert.ToInt32(Request.Form["CenterCode"]);
            string StationCode = Request.Form["StationCode"].ToString(); 
            info.ApplyID = DateTime.Now.AddSeconds(1).ToString("yyyyMMddHHmmss") + ApplyUserID;
            info.ApplyUserID = ApplyUserID;
            info.ApplyUserName = ApplyUserName;
            info.CenterCode = CenterCode;
            info.StationCode = StationCode;
            info.ApprovalState = "申领中";
            info.ApprovalCount = 0;
           
            int res = new I_InvoiceApplyBLL().Add(info);
            if (res > 0)
                return this.JsonResult(Utility.E_JsonResult.OK, "保存成功！", null, null);
            else
                return this.JsonResult(Utility.E_JsonResult.Error, "保存失败", null, null);
        }

        //查询申请详细列表
        public ActionResult GetApplyList()
        {
            try
            {
                DateTime start = Convert.ToDateTime(Request.Form["StartDate"]);
                DateTime end = Convert.ToDateTime(Request.Form["EndDate"]);
                string center = Request.Form["CenterID"].ToString();
                string station = Request.Form["StationID"].ToString();
                string state = Request.Form["State"].ToString();
                //获取页容量
                int rows = int.Parse(Request.Form["rows"]);
                //获取请求的页码
                int page = int.Parse(Request.Form["page"]);
                E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
                string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
                string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;

                var list = new I_InvoiceApplyBLL().GetApplyList(page, rows, start, end, state,center,
                                                                station,em, selfCenterID, selfStationID);
                JsonResult j = this.Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
                return j;
            }

            catch (Exception e)
            {
                Dictionary<string, string> dict = new Dictionary<string, string>();
                dict.Add("InfoID", "0");
                dict.Add("InfoMessage", e.Message);
                return this.Json(dict);
            }
        }
           
    }
}
